package com.rmb.api.service.impl;

import com.rmb.api.mapper.*;
import com.rmb.api.service.MartApiEssentialService;
import com.rmb.mart.entity.api.*;
import com.rmb.common.model.sensitive.Desensitization;
import com.rmb.mart.entity.userinfo.UserCompany;
import com.rmb.mart.qurey.QueryPage;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * (MartApiEssential)表服务实现类
 *
 * @author makejava
 * @since 2021-01-20 09:37:35
 */
@Service("martApiEssentialService")
public class MartApiEssentialServiceImpl implements MartApiEssentialService {
    @Resource
    private MartApiEssentialMapper martApiEssentialMapper;

    @Resource
    private MartApiRecordMapper martApiRecordMapper;

    @Resource
    private MartApiRuleMapper martApiRuleMapper;

    @Resource
    private MartApiChargeMapper martApiChargeMapper;

    @Resource
    private LogAddressDao logAddressDao;

    @Resource
    private MartApiErrorMapper martApiErrorMapper;

    @Resource
    private MartApiCompanyMapper martApiCompanyMapper;

    @Resource
    private MartApiErrorApiMapper martApiErrorApiMapper;


    @Resource
    private MartApiTypeMapper martApiTypeMapper;

    /**
     * 查询接口数据+模糊查询+分页
     *
     * @param martApiEssential 模糊对象
     * @return 对象列表
     */
    @Override
    public List<Map<String, Object>> queryAllByLimit(MartApiEssential martApiEssential) {
        return this.martApiEssentialMapper.queryAllByLimit(martApiEssential);
    }


    /**
     * 接口详情
     * @param apiId
     * @return
     */
    @Override
    public List<Map<String, Object>> selectApiRecord(Long apiId) {

        //查询指定接口详情
        List<Map<String, Object>> apiRecordList = martApiRecordMapper.selectApiRecordById(apiId);

        List<Map<String, Object>> selectApiEssential = new ArrayList<>(apiRecordList);

        //查询接口入参
        MartApiEssential martApiEssential = martApiEssentialMapper.queryById(apiId);
        Map<String, Object> apiEssentialMap = new HashMap<>(16);

        apiEssentialMap.put(apiId+"",martApiEssential);

        selectApiEssential.add(apiEssentialMap);


        return selectApiEssential;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public MartApiEssential queryById(Long id) {
        return this.martApiEssentialMapper.queryById(id);
    }

    /**
     * 修改指定Api数据
     *
     * @param martApiEssential 实例对象
     * @return 受影响行数
     */
    @Override
    public int updateApiEssential(MartApiEssential martApiEssential) {
        martApiEssential.setUpdateBy(Desensitization.MY_NAME);
        martApiEssential.setUpdateTime(new Date());

        return this.martApiEssentialMapper.updateApiEssential(martApiEssential);
    }

    /**
     * 更改接口状态
     *
     * @param apiId  接口ID
     * @param status 接口状态
     * @return 受影响行数
     */
    @Override
    public int updateApiStatus(Integer apiId, Integer status) {
        return martApiEssentialMapper.updateApiStatus(apiId, status);
    }

    /**
     * 删除指定接口
     *
     * @param apiId 接口ID
     * @return 受影响行数
     */
    @Override
    public int removeApiEssentialById(Long apiId) {
        //逻辑删除指定接口
        int apiEssentialApi = martApiEssentialMapper.removeApiEssentialById(apiId);

        //逻辑删除所属企业接口
        int apiCompany = martApiCompanyMapper.removeApiEssentialById(apiId);

        //逻辑删除接口所属错误码
        int apiErrorApi = martApiErrorApiMapper.removeApiEssentialById(apiId);

        return apiEssentialApi + apiCompany + apiErrorApi;
    }

    /**
     * 新增Api数据
     *
     * @param martApiEssential 实例对象
     * @return 实例对象
     */
    @Override
    public int addApiEssential(MartApiEssential martApiEssential) {

        martApiEssential.setCreateBy(Desensitization.MY_NAME);
        martApiEssential.setCreateTime(new Date());
        return this.martApiEssentialMapper.addApiEssential(martApiEssential);
    }

    /**
     * 查询接口规则
     *
     * @return 接口规则集合
     * @param queryPage
     */
    @Override
    public List<MartApiRule> selectAllRule(QueryPage queryPage) {
        return martApiRuleMapper.queryAll(queryPage);
    }

    /**
     * 修改回显规则
     *
     * @param apiRuleId 接口规则
     * @return 指定规则
     */
    @Override
    public MartApiRule selectApiRuleById(Long apiRuleId) {
        return martApiRuleMapper.queryById(apiRuleId);
    }

    /**
     * 编辑Api接口规则数据
     *
     * @param martApiRule 实例对象
     * @return 受影响行数
     */
    @Override
    public int updateApiRule(MartApiRule martApiRule) {
        martApiRule.setUpdateBy(Desensitization.MY_NAME);
        martApiRule.setUpdateCreate(new Date());
        return martApiRuleMapper.update(martApiRule);
    }

    /**
     * 更改接口规则状态
     *
     * @param apiRuleId 接口规则ID
     * @param status    规则状态
     * @return 受影响行数
     */
    @Override
    public int updateApiRuleStatus(Integer apiRuleId, Integer status) {
        return martApiRuleMapper.updateApiRuleStatus(apiRuleId, status);
    }

    /**
     * 接口计费方式
     *
     * @param martApiCharge 实例对象
     * @return 计费详情集合
     */
    @Override
    public List<Map<String, Object>> selectAllCharge(MartApiCharge martApiCharge) {
        return martApiChargeMapper.selectAllCharge(martApiCharge);
    }

    /**
     * 删除指定接口规则
     *
     * @param apiRuleId 接口ID
     * @return 受影响行数
     */
    @Override
    public int removeApiRuleById(Long apiRuleId) {
        return martApiRuleMapper.deleteById(apiRuleId);
    }


    /**
     * 查询数据统计量
     * 1.当天数据
     * 2.数据总量
     * 3.近15日每天数据量
     *
     * @return 放入list结果集
     */
    @Override
    public List<Map<String, Object>> selectStatistics(Long id) {

        //当天数据量
        Map<String, Object> selectQueryCount =
                logAddressDao.selectQueryCount(id);
        //数据总量
        Map<String, Object> selectTodayQueryCount =
                logAddressDao.selectTodayQueryCount(id);
        //十五天内每天数据量
        List<Map<String, Object>> selectFifteenQueryCount =
                logAddressDao.selectFifteenQueryCount(id);

        selectFifteenQueryCount.add(selectQueryCount);
        selectFifteenQueryCount.add(selectTodayQueryCount);
        //存入list返回
        return selectFifteenQueryCount;
    }

    /**
     * 查询错误码参照
     *
     * @return
     */
    @Override
    public List<MartApiError> selectErrorQuery() {
        return martApiErrorMapper.selectErrorQuery();
    }


    /**
     * 查询企业下所有接口
     *
     * @param id
     * @return
     */
    @Override
    public List<MartApiEssential> findByCompanyId(Long id) {
        UserCompany userCompany = martApiCompanyMapper.findById(id);

        List<MartApiCompany> companies = martApiCompanyMapper.findByCompanyIdAndEssentialId(userCompany.getId());
        List<MartApiEssential> list = new ArrayList<>();

        companies.forEach(martApiCompany -> {
            MartApiEssential martApiEssential = martApiEssentialMapper.queryById(martApiCompany.getMartApiEssentialId());
            MartApiRecord martApiRecord = martApiRecordMapper.findByCompanyNameAndEssentialId(userCompany.getBusinessName(), martApiCompany.getMartApiEssentialId());
            martApiEssential.setTrueNum(martApiRecord.getTrueNum());
            martApiEssential.setFalseNum(martApiRecord.getFalseNum());
            martApiEssential.setAverageResponseTime(martApiRecord.getAverageResponseTime());
            martApiEssential.setTimesLimit(martApiCompany.getAmount());

            list.add(martApiEssential);
        });

        return list;

    }

    /**
     * 编辑接口计费方式
     * @param martApiCharge 实例对象
     * @return 编辑结果
     */
    @Override
    public int updateApiCharge(MartApiCharge martApiCharge) {

        martApiCharge.setUpdateBy(Desensitization.MY_NAME);
        martApiCharge.setUpdateTime(new Date());

        return martApiChargeMapper.updateApiCharge(martApiCharge);
    }


    /**
     * 查询指定Api计费
     * @param id Api计费ID
     * @return 计费数据
     */
    @Override
    public MartApiCharge selectApiChargeById(Long id) {
        return martApiChargeMapper.queryById(id);
    }

    /**
     * 查询Api类型
     * @param queryPage 分页对象
     * @return 类型集合
     */
    @Override
    public List<Map<String, Object>> selectAllApiType(QueryPage queryPage) {
        List<Map<String,Object>> apiTypeList=new ArrayList<>();
        List<Map<String,Object>> apiRecordList=new ArrayList<>();
        List<Map<String,Object>> api=new ArrayList<>();

        return null;
    }

    /**
     * 修改Api类型
     * @param martApiType 实例对象
     * @return 操作结果
     */
    @Override
    public int updateApiType(MartApiType martApiType) {
        //指定修改人
        martApiType.setUpdateBy(Desensitization.MY_NAME);

        //修改时间
        martApiType.setUpdateTime(new Date());
        return martApiTypeMapper.updateApiType(martApiType);
    }

    /**
     * 根据Api类型查询指定数据
     * @param apiTypeId Api类型ID
     * @return 查询类型数据
     */
    @Override
    public MartApiType selectApiTypeById(Long apiTypeId) {
        return martApiTypeMapper.selectApiTypeById(apiTypeId);
    }

    /**
     * 接口使用量查询
     * @param martApiRecord
     * @return
     */
    @Override
    public List<Map<String,Object>> selectUsageAmount(MartApiRecord martApiRecord) {
        return martApiRecordMapper.selectApiRecord(martApiRecord) ;
    }


}